﻿<?php

/**
 * home actions.
 *
 * @package    sf_sandbox
 * @subpackage home
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class homeActions extends sfActions {

    /**
     * Executes index action
     *
     * @param sfRequest $request A request object
     */
    public function executeIndex(sfWebRequest $request) {


        if ($this->getUser()->hasAttribute('teacher_mode')) {
            $this->forward('home', 'teacher');
        }

        if ($teacher_obj = $this->getUser()->getTeacherObject()) {
            if ($teacher_obj->getIsManager()) {
                $this->redirect('assignment/index');
            } elseif ($teacher_obj->getIsTrainer()) {
                $this->redirect('program/trainerPrograms');
            }
        }
    }

    public function executeAssign_for_long_program(sfWebRequest $request) {
        $lpid = intval($request->getParameter('lpid', false));
        if ($lpid === false || !is_object(LongTermProgramTable::getInstance()->find($lpid)))
            exit;

        $this->last_3_years = AcademicYearTable::getInstance()->getLast3Years();

        $gu = $this->getUser()->getGuardUser();
        $teacher_obj = $gu->getTeacher();
        $this->teacher_mobile = $teacher_obj->getMobile();
        $conditions = $request->getParameter('condition', false);
        $user_id = intval($gu->getUsername());
        $this->teacher_name = $gu->getName();
        $application_obj = Doctrine_Query::create()->from('LongTermProgramApplication')
                ->where('long_program_id = ?', $lpid)->andWhere('teacher_id = ?', $user_id)
                ->fetchOne();

        if (is_object($application_obj)) {
            $this->form = new LongTermProgramApplicationForm($application_obj);
            $this->lock = true;
        } else {
            $this->lock = false;
            $this->form = new LongTermProgramApplicationForm();
        }

        $this->success = false;
        if ($request->isMethod('POST')) {
            $values = $request->getParameter($this->form->getName());
            $values['long_program_id'] = ($lpid);
            $values['teacher_id'] = ($user_id);
            $this->form->bind($values);
            if ($this->form->isValid()) {


                LinkLongProgramConditionTable::getInstance()->
                        createQuery()->delete()
                        ->where('teacher_id = ?', $user_id)->execute();

                if (count($conditions))
                    foreach ($conditions as $k => $one) {
                        $link = new LinkLongProgramCondition();
                        $link->set('teacher_id', $user_id);
                        $link->set('condition_id', $k);
                        $link->set('long_program_id', $lpid);
                        $link->set('answer', $one);
                        try {
                            $link->save();
                        } catch (Exception $e) {
                            
                        }
                    }
                if ($this->form->getObject()->isNew()) {
                    $this->success = 'save';
                } else {
                    $this->success = 'edit';
                }
                $long_app_obj = $this->form->save();

                /* ------------- */
                $prog_name = $request->getParameter('program_name');
                $prog_hours = $request->getParameter('program_hours');

                $prog_name_glue = implode(';', $prog_name);
                $prog_hours_glue = implode(';', $prog_hours);

                $long_app_obj->set('past_program_name', $prog_name_glue);
                $long_app_obj->set('past_program_hours', $prog_hours_glue);

                $long_app_obj->save();
                /* ------------- */

                if ($long_app_obj->getMobile() != $this->teacher_mobile) {
                    $teacher_obj->setMobile($long_app_obj->getMobile());
                    $teacher_obj->save();
                }

                $this->lock = true;
            } else {
                
            }
        }

        $this->conditions = Doctrine_Query::create()->from('LongProgramCondition')
                        ->where('long_program_id = ?', $lpid)->fetchArray();

        $selected_conditions = Doctrine_Query::create()->from('LinkLongProgramCondition')
                        ->where('long_program_id = ?', $lpid)->andWhere('teacher_id = ?', $user_id)->fetchArray();

        $out_conditions = array();

        foreach ($selected_conditions as $one)
            $out_conditions[$one['condition_id']] = $one['answer'];

        $this->selected_conditions = $out_conditions;
    }

    public function executeTeacher($request) {
        $this->component = $request->getParameter('component', 'assigned_programs');
        // check if a course is assigned for the teacher ( user )
        $teacher_id = $this->getUser()->getGuardUser()->getUsername();
        $ay = AcademicYearTable::getInstance()->getDefaults();
        $ayid = $ay['id'];

        $q = Doctrine_query::create()->from('TeacherCourse')
                ->where('teacher_id = ?', $teacher_id)
                ->andWhere('academic_year_id = ?', $ayid);

        $this->has_course = $q->count() > 0 ? true : false;
    }

    public function executeGet_component($request) {
        $component = $request->getParameter('component');
        return $this->renderComponent('home', $component);
    }

    public function executeAjaxSchoolsBySector(sfWebRequest $request) {
        $intg_id = $this->getRequestParameter('intg_id');
        $level_id = $this->getRequestParameter('level_id');
        $school_options = SchoolTable::getInstance()->getSchoolNamesBySector($intg_id, $level_id);

        return $this->renderText(json_encode($school_options));
    }

    public function executeLoginBoxes(sfWebRequest $request) {


        if ($this->getUser()->isAuthenticated())
            $this->forward('home', 'index');
        $this->setLayout('layout');
    }

    public function executeError404() {
        
    }

    // not used anymore ...
    public function executeGlobalFilter(sfWebRequest $request) {
        $this->form = new globalFilterForm();
        if ($request->isMethod(sfWebRequest::POST)) {
            $data = $request->getParameter('global_filter');

            if (isset($data['archive'])) {
                $this->getUser()->setAttribute('kts_global_program_filter', false);
            } else {
                $this->getUser()->setAttribute('kts_global_program_filter', $data);
            }
            $this->redirect('@homepage');
        }
    }

    public function executeAssign_teacher($request) {

        $teacher_id = $this->getUser()->getGuardUser()->getUsername();

        $style = '<style>#sfWebDebug{display:none}</style>';

        $pid = $request->getParameter('program_id');

        $link = new LinkTeacherProgram();
        $link->set('teacher_id', $teacher_id);
        $link->set('program_id', $pid);
        $link->set('assign_status', 1);
        $link->save();
        exit;
        $message = 'Done!';
        return $this->renderText($style . $message);
    }

    public function executeAssign_course_for_teacher($request) {
        $this->form = new sfForm();
        $this->form->setWidget('course', new sfWidgetFormDoctrineChoice(array('model' => 'Course')));

        if ($request->isMethod('POST')) {
            $course_id = $request->getParameter('course');
            $teacher_id = $this->getUser()->getGuardUser()->getUsername();
            $ay = AcademicYearTable::getInstance()->getDefaults();
            $ayid = $ay['id'];
            $obj = new TeacherCourse();
            $obj->set('academic_year_id', $ayid);
            $obj->set('teacher_id', $teacher_id);
            $obj->set('course_id', $course_id);
            $obj->save();
            $this->redirect('home/teacher');
        }
    }

    public function executeTerms_and_conditions($request) {
        $this->lpid = $request->getParameter('lpid');
    }

}